package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketSearchHistory;
import com.cskaoyan.bean.po.MarketSearchHistoryExample;
import com.cskaoyan.bean.usersadmin.SearchHistoryData;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.service.SearchHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: SearchHistoryServiceImpl
 * Description:
 * date: 2022/6/5 21:01
 *
 * @author bruce
 * @since JDK 8
 */
@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public SearchHistoryData query(BaseParam param, Integer userId, String keyword) {
        //开启分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        //添加条件
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);
        //排序规则
        example.setOrderByClause("add_time desc");
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketSearchHistories);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return SearchHistoryData.data(total,pages,param.getLimit(),param.getPage(),marketSearchHistories);
    }
}
